package com.sentiment.sinalgo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;

import com.sentiment.config.Config;
import com.sentiment.database.bean.CrawlInfo;
import com.sentiment.nlpalgo.method.AlgoHttp;
import com.sentiment.tools.Generator;
import com.sentiment.tools.Transaction;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Sinalgoer {

	private static String baseKeyUrl = "https://c.api.weibo.com/2/search/statuses/limited.json";

	public Sinalgoer() {

	}

	public String formatSinaUrl(String baseUrl) {
		String url;
		ArrayList arr = LoadConfigurationXMLFile.loadSinaPara(Config.paraXmlPath);
		Iterator it = arr.iterator();
		url = baseUrl + "?";
		while (it.hasNext()) {
			url += it.next();
			if (it.hasNext()) {
				url += "&";
			}
		}
		System.out.println(url);
		return url;
	}

	public String formatSinaUrl(String baseUrl, String keyword) {
		String url;
		ArrayList arr = LoadConfigurationXMLFile.loadSinaPara(Config.paraXmlPath);
		Iterator it = arr.iterator();
		url = baseUrl + "?";
		while (it.hasNext()) {
			try {
				String s = (String) it.next();
				String[] ss = s.split("=");
				url += ss[0]+"=" + URLEncoder.encode(ss[1], "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

			url += "&";

		}
		try {
			System.out.println(keyword);
			url = url + "q=" + URLEncoder.encode(keyword, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println(url);
		return url;
	}

	public String htGet(String url) {
		String jsonString = "";
		HttpGet getMethod = new HttpGet(url);
		HttpClient httpClient = HttpClients.createDefault();
		try {
			HttpResponse response = httpClient.execute(getMethod); // 发起GET请求

			int code = response.getStatusLine().getStatusCode(); // 获取返回code

			if (code == 200) { // 成功

				HttpEntity httpEntity = response.getEntity();
				BufferedReader bufferedReader = null;
				StringBuilder entityStringBuilder = new StringBuilder();
				if (httpEntity != null) {
					bufferedReader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"),
							8 * 1024);
					String line = null;
					while ((line = bufferedReader.readLine()) != null) {
						entityStringBuilder.append(line);
					}
					jsonString = entityStringBuilder.toString();

				}
			} else {
				System.out.println("网络链接出错，请查看相关代码等" + code);
				return null;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsonString;
	}

	/**

	 * 传人数据进行新浪关键字搜索api调用 暂时将获取到的数据直接存入CrawlInfo， 待改进将数据单独存入内存的对象中 key:关键字 返回 ：

	 * 不完整的CrawlInfo ，需要后续工作

	 */

	@SuppressWarnings({ "unchecked", "deprecation" })
	public List<CrawlInfo> startSinaKeyword(String key) {
		List<CrawlInfo> list = new ArrayList<CrawlInfo>();
		String url;
		url = formatSinaUrl(baseKeyUrl, key);
		String str = htGet(url);
		if (str == null ||str.contains("filter") || str =="")
			return null;
		JSONObject jobj = JSONObject.fromObject(str);
		JSONArray arr = (JSONArray) jobj.get("statuses");
		if(arr == null){
			return null;
		}
		Iterator<JSONObject> it = arr.iterator();
		Date date = new Date();
		while (it.hasNext()) {
			JSONObject jon = (JSONObject) it.next();
			AlgoHttp algoHttp = new AlgoHttp();
			List<String> text = Transaction.string2List(jon.getString("text"));
			CrawlInfo info = new CrawlInfo(date, null, "微博",
					// 此处标题需要到外部再设置，待插入到数据库中时设置

					null, text, jon.getJSONObject("user").getString("url"), jon.getJSONObject("user").getString("name"),
					new Date(jon.getString("created_at")),
					// 转载次数，访问量，评论量，舆情导向

					jon.getInt("reposts_count"), Generator.generateInt(0, 1000), jon.getInt("comments_count"), 0.0);
			if (Config.useNlp) {
				info.setSentiment(algoHttp.sentiment(text, AlgoHttp.SENTIMENT_WEIBO).get(0).getData1());
			} else
				info.setSentiment(Generator.genenrateDouble(0, 1));
			list.add(info);
		}
		System.out.println(list);
		return list;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		Sinalgoer sina = new Sinalgoer();
		sina.startSinaKeyword("英国地图");
	}
}